package com.atguigu.dga.score.service.impl;

import com.atguigu.dga.assess.bean.GovernanceType;
import com.atguigu.dga.assess.service.GovernanceTypeService;
import com.atguigu.dga.score.bean.GovernanceAssessTable;
import com.atguigu.dga.score.mapper.GovernanceAssessTableMapper;
import com.atguigu.dga.score.service.GovernanceAssessTableService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 * 表治理考评情况 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2023-06-15
 */
@Service
public class GovernanceAssessTableServiceImpl extends ServiceImpl<GovernanceAssessTableMapper, GovernanceAssessTable> implements GovernanceAssessTableService {
    private Map<String, BigDecimal> weightMap = new HashMap<>();
    /*
        套路:
            1.先删。 避免重复考评，造成数据重复
            2.查，算
            3.存
     */
    @Override
    public void saveTableScore(String assessDate) throws Exception {

        //删除旧数据
        remove(new QueryWrapper<GovernanceAssessTable>().eq("assess_date",assessDate));

        //查询
        List<GovernanceAssessTable> allTableScore = baseMapper.getAllTableScore(assessDate);

        //查询权重
        List<GovernanceType> list = typeService.list();
        for (GovernanceType governanceType : list) {
            weightMap.put(governanceType.getTypeCode(),governanceType.getTypeWeight());
        }

        //补上权重分
        for (GovernanceAssessTable assessTable : allTableScore) {
            assessTable.setScoreOnTypeWeight(calWeightScore(assessTable));
        }

        //存入数据库
        saveBatch(allTableScore);

    }

    @Autowired
    private GovernanceTypeService typeService;

    /*
        计算权重分数.
            1.查询到每一类的权重指标
                weightMap: {
                            "SPEC": 20,
                            "CALC": 15
                     }

            2.从GovernanceAssessTable中取出对应类型的分数 * 权重
     */
    private BigDecimal calWeightScore(GovernanceAssessTable assessTable) throws Exception {

        BigDecimal totalScore = BigDecimal.ZERO;

        //计算每一项权重的分数    百分制
        Set<String> keys = weightMap.keySet();
        for (String key : keys) {
            totalScore = totalScore.add(getFieldScore(key,assessTable).multiply(weightMap.get(key)).movePointLeft(1));

        }
        return totalScore;
    }

    /*
        目的是执行:  GovernanceAssessTable类的  getXxx()

        Xxx 和 key之间有以下关系:
            key: CALC


     */
    private BigDecimal getFieldScore(String key,GovernanceAssessTable assessTable) throws Exception {

        String lowerCaseWeight = key.toLowerCase();
        // Calc
        String methodName = "getScore" + lowerCaseWeight.substring(0, 1).toUpperCase() + lowerCaseWeight.substring(1) + "Avg";
        //反射 根据方法名获取某个对象的方法实例，执行
        Class<? extends GovernanceAssessTable> clazz = assessTable.getClass();
        Method method = clazz.getMethod(methodName);
        BigDecimal score = (BigDecimal) method.invoke(assessTable);
        return score;

    }
}
